var ns,app;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        form: {
            pageindex: 0,
            pagesize: 20,
            startTime: '',
            endTime: '',
            activeDate: 0,
            workResultID:0,
            ID:0,
        },
        WorkResultList:[
            { "ID": "0", "Name": "全部" },
            { "ID": "1", "Name": "正常" },
            { "ID": "2", "Name": "异常" },
            { "ID": "3", "Name": "漏巡" },
            { "ID": "4", "Name": "补打" },
        ],
        TotalCount:0,
        list:[],
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.getAnalysisData();
        },
        doSelectWorkResult: function (ID) {
            var that = this;
            that.form.workResultID=ID;
        },
        open_details: function(item) {
            ns.openWin('patrolplan_details_frm_task', '工作记录详情', {
                id: item.ID
            });
        },
        getAnalysisData: function () {
            var that = this;
            that.is_searching= true;
            that.can_scroll = false;
            var options = {};
            options.action = "getpatroltasklistbypatrolid";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.ID = that.form.ID;    
            options.StartTime=that.form.startTime;
            options.EndTime=that.form.endTime;
            options.Status=that.form.workResultID;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {
                    that.TotalCount=data.data.count;
                    if (data.data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data.list;
                    } else {
                        that.list = that.list.concat(data.data.list);
                    }
                    app.current_item_length = that.list.length
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        doReset: function () {
            var that = this;
            that.form.startTime = '';
            that.form.endTime = '';
            that.form.activeDate = 0;
            that.form.activeDateDesc = '时间';
            that.form.workResultID = 0;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        doSelectSearchParam: function (type) {
            var that = this;
            that.showsearchparam = !that.showsearchparam;
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            setTimeout(function () {
                that.initPicker();
            }, 100);
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function (dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(3, 'days').format("YYYY-MM-DD");
                endDate =  dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三天';
            }
            if (dataValue == 2) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(7, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近七天';
            }
            if (dataValue == 3) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(30, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三十天';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.id || 0;
    app.doClickDay(3);
    app.doSelectWorkResult(app.WorkResultList[0].ID);

    setTimeout(function () {
        app.get_data();
    }, 500)
    ns.init_pull();
};
